package com.terrynoya.fight.combat
{
	import com.terrynoya.common.util.DisplayUtil;
	import com.terrynoya.fight.char.Character;
	import com.terrynoya.fight.char.Entity;
	import com.terrynoya.fight.char.ICharaterRender;
	import com.terrynoya.fight.emuns.PositionType;
	import com.terrynoya.fight.util.Misc;
	import com.terrynoya.geom.MVector2D;
	
	import flash.display.Sprite;
	
	public class Helper extends Character
	{
		private var _parent:Character;
		private var _data:HelperData;
		private var _firstTick:Boolean;
		private var _offsetCharacter:Character;
		private var _view:Sprite;
		private var _canRemove:Boolean;
		
		public function Helper(engine:FightEngine,parent:Character,data:HelperData)
		{
			super(engine);
			this._view = new Sprite();
			this._parent = parent;
			this.render = parent.render;
			this._data = data;
			this._firstTick = true;	
			this.stateManager = parent.stateManager.clone(this);
			this.animationManager = parent.animationManager.clone();
			this._offsetCharacter = data.positionType == PositionType.P2 ? parent.getOpponent() : parent;
			this.setLocalAnimation(0,0);
			this.stateManager.changeState(data.initialStateId);
		}
		
		public function get data():HelperData
		{
			return _data;
		}

		override public function getBasePlayer():Character
		{
			return this._parent;
		}
		
		override public function draw():void
		{
			this.render.draw(this,this._view);
		}
		
		override public function updateState():void
		{
			if(this._firstTick)
			{
				this.engine.stage.characterLayer.addChild(this._view);
				this.position = this.getStartLocation();
				this._firstTick = false;
			}
			super.updateState();
		}
		
		override public function remove():void
		{
			this._canRemove = true;
			DisplayUtil.removeFromParent(this._view);
			this._parent.removeHelper(this);
		}
		
		override public function removeCheck():Boolean
		{
			return this._canRemove;
		}
		
		private function getStartLocation():MVector2D
		{
			var facing:int = this._offsetCharacter.facing;
			var pos:MVector2D;
			switch(this._data.positionType)
			{
				case PositionType.P1:
				case PositionType.P2:
				{
					return Misc.getOffset(this._offsetCharacter.position,facing,this._data.creationOffset);
					break;
				}
			}
			return pos;
		}
	}
}